package com.aliyun.heiyu.powermaster.server.strategy.analyzer;

import com.aliyun.heiyu.common.util.LogUtils;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.proto.PowerStrategy;
import com.aliyun.heiyu.powermaster.proto.StrategyAction;
import com.aliyun.heiyu.powermaster.server.MasterConstant;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManager;
import com.aliyun.heiyu.powermaster.server.manager.ThunderManagerImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class AICstateStrategyAnalyzer extends StrategyAnalyzer {
    private static Logger logger = LoggerFactory.getLogger(AICstateStrategyAnalyzer.class);

    private ThunderManager thunderManager;

    public AICstateStrategyAnalyzer(){
        super(MasterConstant.AI_CSTATE, PowerStrategy.CSTATE.name());
        this.thunderManager = new ThunderManagerImpl();
    }

    @Override
    protected Boolean monitorCondition(StrategyAction strategyAction, String sn, String level) {
        logger.debug("监控设备  == "+ LogUtils.vaildlog(sn)+"cstate条件");
        if("0".equals(level) || "1".equals(level)){
            return statusJudge(sn);
        }else if("3".equals(level)){
            return cstateJudge(sn);
        }else if("2".equals(level)){
            return cstateJudge(sn);
        }else {
            return null;
        }
    }

    @Override
    protected Boolean isTrue(String sn) {
        Boolean status = thunderManager.getStatusOfCstate(sn);
        return status;
    }

    private Boolean statusJudge(String sn){
        Boolean status = thunderManager.getStatusOfCstate(sn);
        if(status != null && status){
            return false;
        }
        return null;
    }

    private Boolean cstateJudge(String sn){
        Boolean status = thunderManager.getStatusOfCstate(sn);
        if(status == null){
            return null;
        }
        //优先判断sla是否受损,并且capping状态应该关闭，否则不动作
        if(monitor.isQOSDamaged(sn)){
            if(status){
                return Boolean.FALSE;
            }
            return null;
        }

        //前一个监控点
        Boolean b1 = null;
        String cpuUsage1 = monitor.getCPUUsage(sn,-1);
        if(!StringUtil.isEmpty(cpuUsage1)){
            b1 = Double.parseDouble(cpuUsage1) >= CPU_USAGE;
        }

        //当前监控点
        Boolean b2 = null;
        String cpuUsage2 = monitor.getCPUUsage(sn,0);
        if(!StringUtil.isEmpty(cpuUsage2)){
            b2 = Double.parseDouble(cpuUsage2) >= CPU_USAGE;
        }

        //下一个监控点
        Boolean b3 = null;
        String cpuUsage3 = monitor.getCPUUsage(sn,1);
        if(!StringUtil.isEmpty(cpuUsage3)){
            b3 = Double.parseDouble(cpuUsage3) >= CPU_USAGE;
        }

        if(b3 == null){
            b3 = false;
        }
        if(b1 == null || b2 == null){
            logger.debug("有条件为 NULL， cstate 不进行动作");
            return null;
        }
        if(b1 && b2){
            logger.debug("条件满足， un-cstate 进行动作");
            if(status == null || status){
                return Boolean.FALSE;
            }
            return null;
        } else if(b2 && b3){
            logger.debug("条件满足ai， un-cstate 进行动作");
            if(status == null || status){
                return Boolean.FALSE;
            }
            return null;
        }
        else {
            logger.debug("条件满足， cstate 进行动作");
            if(status == null || !status){
                return Boolean.TRUE;
            }
            return null;
        }
    }
}
